
/**
 * pk模块的消息管理
 * 1：定义消息标识
 * 2：发送请求
 * 3：侦听请求，并处理、转发响应
 * 该类中不应该应用其它应用模块的类
 * 
 */
package com.mj.model
{
	import com.gamebase.net.rpc.Session;
	import com.gamebase.net.rpc.baseTypeEx.int16;
	import com.gamebase.net.rpc.baseTypeEx.uint16;
	import com.mj.ASDebugger;
	import com.mj.vo.AreaData;
	import com.mj.vo.CSLoginReq;
	import com.mj.vo.Card;
	import com.mj.vo.RoleData;
	import com.mj.vo.SCMJ2EnterRoomRes;
	import com.model.Model;
	import com.notice.NoticeSC;
	import com.socket.SocketBridge;
	import com.socket.SocketManager;
	
	import flash.events.Event;
	
	public class MjModel extends Model
	{
		private static var _instance:MjModel;
		private var session:Session;
		private var _onAreaInfo:Function;
		private var _onConnect:Function;
		
		
		
		public function MjModel(single:SingleTon)
		{
			super();
			
		}
		
		public static function get I():MjModel
		{
			if(_instance == null)
			{
				_instance = new MjModel(new SingleTon());
			}
			return _instance;
		}
		
		public function connect($onConnect:Function):void
		{
			
			_onConnect = $onConnect;
			var balance:Balance = new Balance();
			balance.connectServer(onSocketConnect, onSocketClose);
		}
		
		private function onSocketConnect(evt:Event):void
		{
			session = Session.instance;
			session.addEventListener(ProtocolMap.GATEWAY_CMD_0+","+ProtocolMap.SC_LOGIN_RES, onLoginGame);
			session.addEventListener(ProtocolMap.GATEWAY_CMD_0+","+ProtocolMap.SC_GET_AREA_RES, onReqArea);
			session.addEventListener(ProtocolMap.MJ2_CMD_0+","+ProtocolMap.SC_MJ2_ENTER_ROOM_RES, onEnterArea);
			session.addEventListener(ProtocolMap.MJ2_CMD_0+","+ProtocolMap.SC_NOTIFY_MJ2_ENTER_ROOM, otherJoin);
			session.addEventListener(ProtocolMap.MJ2_CMD_0+","+ProtocolMap.SC_MJ2_READY_RES, selfReady);
			session.addEventListener(ProtocolMap.MJ2_CMD_0+","+ProtocolMap.SC_MJ2_NOTIFY_READY, otherReady);
			session.addEventListener(ProtocolMap.MJ2_CMD_0+","+ProtocolMap.SC_NOTIFY_MJ2_BANKER, onBanker);
			session.addEventListener(ProtocolMap.MJ2_CMD_0+","+ProtocolMap.SC_NOTIFY_MJ2_START_POS, onPos);
			session.addEventListener(ProtocolMap.MJ2_CMD_0+","+ProtocolMap.SC_NOTIFY_MJ2_INIT_CARD, onCardInfo);
			session.addEventListener(ProtocolMap.MJ2_CMD_0+","+ProtocolMap.SC_MJ2_GET_CARD_RES, onTakeCard);
			session.addEventListener(ProtocolMap.MJ2_CMD_0+","+ProtocolMap.SC_NOTIFY_MJ2_GET_CARD, onOtherTakeCard);
			session.addEventListener(ProtocolMap.MJ2_CMD_0+","+ProtocolMap.SC_NOTIFY_MJ2_PUT_CARD, onOtherPutCard);
			
			
			if(_onConnect != null)
			{
				_onConnect(null);
			}
		}
		
		private function onSocketClose(evt:Event):void
		{
			trace("socket连接断开");
		}
		
		//============================ c-s ================================
		//登录游戏服务器
		public function loginGame($openId:String, $openKey:String, onAreaInfo:Function):void
		{
			_onAreaInfo = onAreaInfo;
			traceObjMsg("----登录游戏服务器");
			var loginData:CSLoginReq = new CSLoginReq();
			loginData.openId = $openId; 
			loginData.openKey = $openKey;
			
			session.request(ProtocolMap.GATEWAY_CMD_0, ProtocolMap.CS_LOGIN_REQ, loginData);
		}
		
		public function enterArea($areaId:uint16):void
		{
			session.request(ProtocolMap.MJ2_CMD_0, ProtocolMap.CS_MJ2_ENTER_ROOM_REQ, $areaId);
		}
		
		public function getReady():void
		{
			session.request(ProtocolMap.MJ2_CMD_0, ProtocolMap.CS_MJ2_READY_REQ);
		}
		
		public function takeCard():void
		{
			session.request(ProtocolMap.MJ2_CMD_0, ProtocolMap.CS_MJ2_GET_CARD_REQ);
		}
		
		public function selfPutCard($card:Card):void
		{
			session.request(ProtocolMap.MJ2_CMD_0, ProtocolMap.CS_MJ2_PUT_CARD_REQ, $card);
		}
		
		//============================ s-c ================================
		
		//登录游戏服务器返回
		private function onLoginGame(data:RoleData):void
		{
			traceObjMsg("++++登陆游戏房间"+ProtocolMap.SC_LOGIN_RES+": ", data);
			CommonData.selfInfo = data;
			session.setValidate(data.role_id);
			session.request(ProtocolMap.GATEWAY_CMD_0, ProtocolMap.CS_GET_AREA_REQ);
			
		}
		
		//获得区消息
		private function onReqArea(data:Vector.<AreaData>):void
		{
			traceObjMsg("++++游戏区信息："+ProtocolMap.SC_GET_AREA_RES+": ", data);
			_onAreaInfo(data);
			
		}
		
		//加入游戏区
		private function onEnterArea(data:SCMJ2EnterRoomRes):void
		{
			traceObjMsg("++++加入游戏区："+ProtocolMap.SC_GET_AREA_RES+": ", data);
			this.dispatch(ProtocolMap.SC_MJ2_ENTER_ROOM_RES,data);
			
		}
		
		//玩伴加入游戏
		private function otherJoin($data:RoleData):void
		{
			traceObjMsg("++++玩伴加入游戏："+ProtocolMap.SC_NOTIFY_MJ2_ENTER_ROOM+": ", $data);
			this.dispatch(ProtocolMap.SC_NOTIFY_MJ2_ENTER_ROOM,$data);
			
		}
		
		//自己准备
		private function selfReady():void
		{
			traceObjMsg("++++自己准备："+ProtocolMap.SC_MJ2_READY_RES+": ", "");
			this.dispatch(ProtocolMap.SC_MJ2_READY_RES);
		}
		
		//玩伴准备
		private function otherReady():void
		{
			traceObjMsg("++++玩伴准备："+ProtocolMap.SC_MJ2_NOTIFY_READY+": ", "");
			this.dispatch(ProtocolMap.SC_MJ2_NOTIFY_READY);
		}
		
		//定庄家
		private function onBanker(numV:Vector.<int16>):void
		{
			traceObjMsg("++++定庄家："+ProtocolMap.SC_NOTIFY_MJ2_BANKER+": ", "");
			this.dispatch(ProtocolMap.SC_NOTIFY_MJ2_BANKER, numV);
		}
		
		//定位置
		private function onPos(pos:Vector.<int16>):void
		{
			traceObjMsg("++++定位置："+ProtocolMap.SC_NOTIFY_MJ2_START_POS+": ", "");
			this.dispatch(ProtocolMap.SC_NOTIFY_MJ2_START_POS, pos);
		}
		
		//获取到牌信息
		private function onCardInfo(cardInfo:Vector.<Card>):void
		{
			traceObjMsg("++++获取到牌信息："+ProtocolMap.SC_NOTIFY_MJ2_INIT_CARD+": ", "");
			this.dispatch(ProtocolMap.SC_NOTIFY_MJ2_INIT_CARD, cardInfo);
		}
		
		//抓牌响应
		private function onTakeCard(card:Card):void
		{
			this.dispatch(ProtocolMap.SC_MJ2_GET_CARD_RES, card);
		}
		
		//其他人抓牌
		private function onOtherTakeCard(card:Card):void
		{
			this.dispatch(ProtocolMap.SC_NOTIFY_MJ2_GET_CARD, card);
		}
		
		//其他人出牌
		private function onOtherPutCard(card:Card):void
		{
			this.dispatch(ProtocolMap.SC_NOTIFY_MJ2_PUT_CARD, card);
		}
		
		private function traceObjMsg(preStr:String, $data:Object=null):void
		{
			var msg:String = "";
			if($data != null)
			{
				for(var att:Object in $data)
					msg += att.toString()+":"+$data[att]+ " , ";
			}
			
			//ASDebugger.debug(preStr + msg);
			trace(preStr + msg);
		}
		
	}
}

class SingleTon{}